<?php

namespace app\model;

class LineModel extends BaseModel
{
    protected $name = 'line';


    public const STATUS_DISABLED = 100;  //禁用
    public const STATUS_ENABLED = 200; //启用

    public const STATUS_TEXT = [
        self::STATUS_DISABLED => '禁用',
        self::STATUS_ENABLED => '启用',
    ];

    // 追加属性
    protected $append = [
        'status_text',
    ];


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        return self::STATUS_TEXT[$value] ?? '';
    }

    /**
     * Notes: 分页
     * User: chenLei
     * Date: 10/13/25
     * @param $params
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function list($params)
    {
        $where = [];
        if (!empty($params['name'])) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }
        if (!empty($params['status'])) {
            $where[] = ['status', '=', $params['status']];
        }

        return $this->where($where)->order('id', 'desc')->paginate($params['page_size'] ?? 20);
    }

    /**
     * Notes: 所有列表
     * User: chenLei
     * Date: 10/13/25
     * @param $params
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function all($params)
    {
        $where = [];
        if (!empty($params['name'])) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }
        if (!empty($params['status'])) {
            $where[] = ['status', '=', $params['status']];
        }
        return $this->where($where)->order('id', 'desc')->select();
    }


    /**
     * Notes: 添加
     * User: chenLei
     * Date: 10/13/25
     * @param $params
     * @return bool
     */
    public function add($params)
    {
        if ($this->where('name', $params['name'])->count()) {
            $this->error = '该名称已经存在';
            return false;
        }

        $this->save([
            'name' => $params['name'],
            'status' => $params['status'],
        ]);

        return true;
    }


    /**
     * Notes: 编辑
     * User: chenLei
     * Date: 10/13/25
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit($params)
    {
        $info = $this->where('id', $params['id'])->find();
        if (empty($info)) {
            $this->error = '该数据不存在';
            return false;
        }

        if ($this->where('name', $params['name'])->where('id', '<>', $params['id'])->count()) {
            $this->error = '该名称已经存在';
            return false;
        }

        $info->save([
            'name' => $params['name'],
            'status' => $params['status'],
        ]);

        return true;
    }


    /**
     * Notes: 删除
     * User: chenLei
     * Date: 10/13/25
     * @param $params
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function del($params)
    {
        $info = $this->where('id', $params['id'])->find();
        if (empty($info)) {
            $this->error = '该数据不存在';
            return false;
        }

        $info->delete();
        return true;
    }
}